/*
 * Created by Jesse Phillips
 * Created on 12-Jan-06
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class ShapeDriver {
	private String inputFile = "P1Input.txt";
	private String errOutputFile = "shapeerrors.txt";
	private String outputFile = "shapes.out";
	private Scanner input, userInput;
	private PrintWriter errOutput, output;
	private ArrayList<TwoDShape> shapes;
	
	public static void main(String[] args) {
		ShapeDriver program = new ShapeDriver();
		program.readInput();
		program.sort();
		
		int cmd;
		
		do {
			System.out.println();
			cmd = program.menu();
			System.out.println();
			
			if(cmd == 1)
				program.printAll();
			if(cmd == 2)
				program.printThis(program.showChoices());
			if(cmd == 3)
				program.printType(program.requestType());
		}while(cmd != 4);
		program.quit();
	}
	
	public ShapeDriver() {
		shapes = new ArrayList<TwoDShape>();
		try {
			userInput = new Scanner(System.in);
			input = new Scanner(new File(inputFile));
			errOutput = new PrintWriter(new File(errOutputFile));
			output = new PrintWriter(new File(outputFile));
		} catch (FileNotFoundException e) {
			System.out.println("File not found. Quiting\n");
			e.printStackTrace();
			System.exit(1);
		}
	}
	
	public void readInput() {
		while(input.hasNext()) {
			String shape = input.next();
			try {
				if(shape.equalsIgnoreCase("Rectangle")) {
					input.nextLine();
					shapes.add(readRect());
				}if(shape.equalsIgnoreCase("Square")) {
					input.nextLine();
					shapes.add(readSquare());
				}if(shape.equalsIgnoreCase("Circle")) {
					input.nextLine();
					shapes.add(readCircle());
				}if(shape.equalsIgnoreCase("Triangle")) {
					input.nextLine();
					shapes.add(readTri());
				}
			}catch(ShapeException se) {
				errOutput.println(se.getMessage()+" "+se.getShapeName()+" not added.");
				errOutput.println(se.getError());
				errOutput.println(se.getValues()+"\n");
			}
		}
		errOutput.close();
		input.close();
	}
	
	private Rectangle readRect() throws ShapeException {
		return Rectangle.create(input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble());	
	}
	
	private Square readSquare() throws ShapeException {
		return Square.create(input.nextDouble(), input.nextDouble(), input.nextDouble(), input.nextDouble());
	}
	
	private Circle readCircle() throws ShapeException {
		return Circle.create(input.nextDouble());
	}
	
	private Triangle readTri() throws ShapeException {
		return Triangle.create(input.nextDouble(), input.nextDouble(), input.nextDouble());	
	}
	
	public void sort() {
		int firstUnsorted = 1;
		TwoDShape insertee;
		
		for(; firstUnsorted < shapes.size(); firstUnsorted++) {
			int i = firstUnsorted-1;
			insertee = shapes.get(firstUnsorted);
			for(; i>-1 && insertee.compareTo(shapes.get(i)) < 0; i--) {
				shapes.set(i+1, shapes.get(i));
			}
			shapes.set(i+1, insertee);
		}
			
		
	}
	
	public int menu() {
		System.out.print("1. Print all\n2. Print #\n3. Print Type\n4. Quit\n# ");
		int cmd = userInput.nextInt();
		return cmd;
	}
	
	public int showChoices() {
		for(TwoDShape shape: shapes)
			System.out.println(shapes.indexOf(shape)+": "+shape.getName());
		System.out.print("# ");
		return userInput.nextInt();
	}
	
	public String requestType() {
		System.out.print("1. Rectangle\n2. Square\n3. Circle\n4. Triangle\n# ");
		int choice = userInput.nextInt();
		
		switch(choice) {
		case 1:
			return "Rectangle";
		case 2:
			return "Square";
		case 3:
			return "Circle";
		case 4: 
			return "Triangle";
		default:
			return null;
		}
	}
	
	public void printAll() {
		for(TwoDShape i : shapes) {
			System.out.println(shapes.indexOf(i)+": "+i+"\n");
			output.println(shapes.indexOf(i)+": "+i+"\n");
		}
	}

	public void quit() {
		output.close();
	}
	
	public void printThis(int index) {
		System.out.println(index+": "+shapes.get(index));
	}
	
	public void printType(String type) {
		for(TwoDShape i : shapes) {
			if(i.getName().equalsIgnoreCase(type)) {
				System.out.println(shapes.indexOf(i)+": "+i+"\n");
				output.println(shapes.indexOf(i)+": "+i+"\n");
			}
		}
	}
}
